package com.linx.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linx.common.core.service.MenuTypeService;
import com.linx.common.core.utils.MapstructUtils;
import com.linx.common.core.utils.StringUtils;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.system.domain.AppMenuType;
import com.linx.system.domain.bo.AppMenuTypeBo;
import com.linx.system.domain.vo.AppMenuTypeVo;
import com.linx.system.mapper.AppMenuTypeMapper;
import com.linx.system.service.IAppMenuTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 移动端应用菜单分类Service业务层处理
 *
 * @author linx
 * @date 2024-12-25
 */
@RequiredArgsConstructor
@Service
public class AppMenuTypeServiceImpl implements IAppMenuTypeService , MenuTypeService {

    private final AppMenuTypeMapper baseMapper;

    /**
     * 查询移动端应用菜单分类
     *
     * @param appMenuTypeId 主键
     * @return 移动端应用菜单分类
     */
    @Override
    public AppMenuTypeVo queryById(Long appMenuTypeId){
        return baseMapper.selectVoById(appMenuTypeId);
    }

    /**
     * 分页查询移动端应用菜单分类列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 移动端应用菜单分类分页列表
     */
    @Override
    public TableDataInfo<AppMenuTypeVo> queryPageList(AppMenuTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppMenuType> lqw = buildQueryWrapper(bo);
        Page<AppMenuTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的移动端应用菜单分类列表
     *
     * @param bo 查询条件
     * @return 移动端应用菜单分类列表
     */
    @Override
    public List<AppMenuTypeVo> queryList(AppMenuTypeBo bo) {
        LambdaQueryWrapper<AppMenuType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppMenuType> buildQueryWrapper(AppMenuTypeBo bo) {
        LambdaQueryWrapper<AppMenuType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMenuTypeName()), AppMenuType::getMenuTypeName, bo.getMenuTypeName());
        return lqw;
    }

    /**
     * 新增移动端应用菜单分类
     *
     * @param bo 移动端应用菜单分类
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppMenuTypeBo bo) {
        AppMenuType add = MapstructUtils.convert(bo, AppMenuType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAppMenuTypeId(add.getAppMenuTypeId());
        }
        return flag;
    }

    /**
     * 修改移动端应用菜单分类
     *
     * @param bo 移动端应用菜单分类
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppMenuTypeBo bo) {
        AppMenuType update = MapstructUtils.convert(bo, AppMenuType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppMenuType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除移动端应用菜单分类信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public String selectMenuTypeNameById(Object appMenuTypeId) {
        AppMenuType appMenuType = baseMapper.selectOne(new LambdaQueryWrapper<AppMenuType>()
            .select(AppMenuType::getMenuTypeName).eq(AppMenuType::getAppMenuTypeId, appMenuTypeId));
        return ObjectUtil.isNull(appMenuType) ? null : appMenuType.getMenuTypeName();
    }
}
